﻿#include "OpenGLVertexArray.h"

using namespace Kriko;

OpenGLVertexArray::OpenGLVertexArray() {
    glGenVertexArrays(1, &arrayID);
}

OpenGLVertexArray::~OpenGLVertexArray() {
    for (int i = 0; i < buffers.size(); ++i) {
        delete buffers[i];
    }
    buffers.clear();
    glDeleteVertexArrays(1, &arrayID);
}

void OpenGLVertexArray::AddBuffer(OpenGLBuffer* buffer, GLuint index) {
    Bind();
    buffer->Bind();

    glEnableVertexAttribArray(index);
    glVertexAttribPointer(index, buffer->GetComponentCount(), GL_FLOAT, GL_FALSE, 0, 0);

    buffer->Unbind();
    Unbind();

    buffers.push_back(buffer);
}

void OpenGLVertexArray::Bind() {
    glBindVertexArray(arrayID);
}
void OpenGLVertexArray::Unbind() {
    glBindVertexArray(0);
}